clear all
set more off
capture log close
set matsize 11000 

*---- Set Directory ----*
global rootDir = "/Users/AliceZhang/Dropbox/Research_Columbia/Renewables Voting (Urpelainen Zhang)/JOP/UZ_JOP2021_Replication"
global dataDir = "Data"
global resultDir = "Results"
global logDir = "Analysis/logSTATA"
global graphDir = "Results/Figures"

*---- Log File Path ----*
cd "$rootDir/$logDir"
log using 014_analysis_RPS, replace

*******************************************************************************
/*				          URPELAINEN & ZHANG 2021	        				 */
*******************************************************************************

/* 

File Name:    	012_analysis_Texas.do

By:				Alice Tianbo Zhang (alice.tianbo.zhang@gmail.com)

Last Edited:  	10/20/2021

Purpose:		

Data Used:      election_district_panel_RPS_recodeVar.dta
				votes_wind_panel_RPS_recodeVar.dta
			

Program Used:  

*/

*******************************************************************************
/*               	 			  TABLE A23	 						    	 */
*******************************************************************************
cd "$rootDir/$dataDir/Final"
use election_district_panel_RPS_recodeVar.dta, clear
capture drop postRPS_year
capture drop postRPS_dum

* Create event indicators 
gen postRPS_year = year - start_year_verified
gen postRPS_dum = postRPS_year > 0 & postRPS_year != .
gen RPS_event = postRPS_dum * postRPS_year

* State-specific time trend
capture drop s1-s41
capture drop year1-year41
tab state, gen(s)
local trend
forvalues i=1/41 {
	gen year`i' = s`i' * postRPS_year
	local trend `trend' year`i'
}
di "`trend'"

gen cum_lncapacity_turbine = log(cum_capacity_turbine + 1)
gen cum_lncount_turbine = log(cum_count_turbine +1 )
gen turnout = votes1 + votes2 + votes3 + votes4
gen lnturnout = log(turnout)

*---------------------------- Linear Trend Break -----------------------------*
local wind_outcome cum_capacity_turbine cum_count_turbine cum_lncapacity_turbine cum_lncount_turbine
local election_outcome demvotesmajorpercent repvotesmajorpercent incumbvotesmajorpercent turnout lnturnout

foreach y_var in `wind_outcome'{

	tokenize "`y_var'", parse("_")
	local y_name "`3'"
	di "`y_name'"
	areg `y_var' RPS_event `trend', absorb(state) robust cluster(state)
	estimates store `y_name'_trendBreak
	
}
foreach y_var in `election_outcome'{
	
	local y_name = substr("`y_var'", 1, 3)
	areg `y_var' RPS_event `trend', absorb(state) robust cluster(state)
	estimates store `y_name'_trendBreak
	
}

*----------------------------- Average RPS Effect ----------------------------*
foreach y_var in `wind_outcome'{

	tokenize "`y_var'", parse("_")
	local y_name "`3'"
	di "`y_name'"
	areg `y_var' postRPS_dum `trend', absorb(state) robust cluster(state)
	estimates store `y_name'_avgEffect
	
}

foreach y_var in `election_outcome'{
	
	local y_name = substr("`y_var'", 1, 3)
	areg `y_var' postRPS_dum `trend', absorb(state) robust cluster(state)
	estimates store `y_name'_avgEffect
	
}

*-------------------- Export LaTeX Tables: Results on RPS --------------------*
cd "$rootDir/$resultDir/Tables"

** Effect of RPS on wind turbine construction
* Trend Break
esttab capacity_trendBreak count_trendBreak lncapacity_trendBreak ///
		lncount_trendBreak using TableA23.tex, booktabs replace ///
		keep(RPS_event) ///
		b(%9.3f) se noconstant nonotes star(* 0.10 ** 0.05 *** 0.01) ///
		stats(N N_clust r2, labels("Observations" "States" "\(R^{2}\)") fmt(0 0 2)) ///
		varlabels(RPS_event "RPS $\times$ Event Time") varwidth(27) modelwidth(13) ///
		mtitles("Capacity (MW)" "Count" "Log Capacity" "Log Count") ///
		mgroups("A. Linear Trend Break", pattern(1 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
		width(\hsize)
		

* Average Effect
esttab capacity_avgEffect count_avgEffect lncapacity_avgEffect ///
		lncount_avgEffect using TableA23.tex, booktabs append ///
		keep(postRPS_dum) ///
		se noconstant nonotes legend nonumbers collabels(none) star(* 0.10 ** 0.05 *** 0.01) ///
		b(%9.3f) stats(N N_clust r2, labels("Observations" "States" "\(R^{2}\)") fmt(0 0 2)) ///
		varlabels(postRPS_dum "RPS") varwidth(27) modelwidth(13) ///
		mtitles("Capacity (MW)" "Count" "Log Capacity" "Log Count") ///
		mgroups("B. Average Effect", pattern(1 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
		width(\hsize)

		
*******************************************************************************
/*               	 			  TABLE A24	 						    	 */
*******************************************************************************

** Effect of RPS on election outcomes
* Trend Break
esttab dem_trendBreak rep_trendBreak inc_trendBreak using TableA24.tex, booktabs replace ///
		keep(RPS_event) ///
		b(%9.3f) se noconstant nonotes star(* 0.10 ** 0.05 *** 0.01) ///
		stats(N N_clust r2, labels("Observations" "States" "\(R^{2}\)") fmt(0 0 2)) ///
		varlabels(RPS_event "RPS $\times$ Event Time") varwidth(27) modelwidth(13) ///
		mtitles("Democratic Vote" "Republican Vote" "Incumbent Vote") ///
		mgroups("A. Linear Trend Break", pattern(1 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
		width(\hsize)
		
* Average Effect
esttab dem_avgEffect rep_avgEffect inc_avgEffect using TableA24.tex, booktabs append ///
		keep(postRPS_dum) ///
		se noconstant nonotes legend nonumbers collabels(none) star(* 0.10 ** 0.05 *** 0.01) ///
		b(%9.3f) stats(N N_clust r2, labels("Observations" "States" "\(R^{2}\)") fmt(0 0 2)) ///
		varlabels(postRPS_dum "RPS") varwidth(27) modelwidth(13) ///
		mtitles("Democratic Vote" "Republican Vote" "Incumbent Vote") ///
		mgroups("B. Average Effect", pattern(1 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
		width(\hsize)

		
*******************************************************************************
/*               	 			  TABLE A25	 						    	 */
*******************************************************************************
cd "$rootDir/$dataDir/Final"
use votes_wind_panel_RPS_recodeVar.dta, clear

capture drop postRPS_year
capture drop postRPS_dum

* Create event indicators 
gen postRPS_year = year - start_year_verified
gen postRPS_dum = postRPS_year > 0 & postRPS_year != .
gen RPS_event = postRPS_dum * postRPS_year

* State-specific time trend
capture drop s1-s41
capture drop year1-year41
tab state, gen(s)
local trend
forvalues i=1/41 {
	gen year`i' = s`i' * postRPS_year
	local trend `trend' year`i'
}
di "`trend'"

gen cum_lncapacity_turbine = log(cum_capacity_turbine + 1)
gen cum_lncount_turbine = log(cum_count_turbine +1 )
gen turnout = votes1 + votes2 + votes3 + votes4
gen lnturnout = log(turnout)

*---------------------------- Linear Trend Break -----------------------------*
local wind_outcome cum_capacity_turbine cum_count_turbine cum_lncapacity_turbine cum_lncount_turbine
rename pro_env_clean_energy pro_env_clean
rename pro_env_climate_change pro_env_climate

local environ_outcome pro_env anti_env pro_env_clean pro_env_air pro_env_climate

foreach y_var in `wind_outcome'{

	tokenize "`y_var'", parse("_")
	local y_name "`3'"
	di "`y_name'"
	areg `y_var' RPS_event `trend', absorb(state) robust cluster(state)
	estimates store `y_name'_trendBreak
	
}
foreach y_var in `environ_outcome'{
	areg `y_var' RPS_event `trend', absorb(state) robust cluster(state)
	estimates store `y_var'_trendBreak
	
}

*----------------------------- Average RPS Effect ----------------------------*
foreach y_var in `wind_outcome'{

	tokenize "`y_var'", parse("_")
	local y_name "`3'"
	di "`y_name'"
	areg `y_var' postRPS_dum `trend', absorb(state) robust cluster(state)
	estimates store `y_name'_avgEffect
	
}

foreach y_var in `environ_outcome'{

	areg `y_var' postRPS_dum `trend', absorb(state) robust cluster(state)
	estimates store `y_var'_avgEffect
	
}

*-------------------- Export LaTeX Tables: Results on RPS --------------------*
cd "$rootDir/$resultDir/Tables"
	
** Effect of RPS on environmental voting outcomes
* Trend Break
esttab pro_env_trendBreak pro_env_clean_trendBreak anti_env_trendBreak ///
		using TableA25.tex, booktabs replace ///
		keep(RPS_event) ///
		b(%9.3f) se noconstant nonotes star(* 0.10 ** 0.05 *** 0.01) ///
		stats(N N_clust r2, labels("Observations" "States" "\(R^{2}\)") fmt(0 0 2)) ///
		varlabels(RPS_event "RPS $\times$ Event Time") varwidth(27) modelwidth(13) ///
		mtitles("Pro-Environment Vote" "Pro Clean Energy Vote" "Anti-Environment Vote") ///
		mgroups("A. Linear Trend Break", pattern(1 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
		width(\hsize)
		
* Average Effect
esttab pro_env_avgEffect pro_env_clean_avgEffect anti_env_avgEffect ///
		using TableA25.tex, booktabs append ///
		keep(postRPS_dum) ///
		se noconstant nonotes legend nonumbers collabels(none) star(* 0.10 ** 0.05 *** 0.01) ///
		b(%9.3f) stats(N N_clust r2, labels("Observations" "States" "\(R^{2}\)") fmt(0 0 2)) ///
		varlabels(postRPS_dum "RPS") varwidth(27) modelwidth(13) ///
		mtitles("Pro-Environment Vote" "Pro Clean Energy Vote" "Anti-Environment Vote") ///
		mgroups("B. Average Effect", pattern(1 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
		width(\hsize)

		
** Close log file
log close
cd "$rootDir/$logDir"
translate 014_analysis_RPS.smcl 014_analysis_RPS.log, replace
